package org.dromara.yuanshidian.service.impl;

import org.apache.ibatis.annotations.Param;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.yuanshidian.domain.Ab01;
import org.dromara.yuanshidian.domain.vo.Ab01Vo;
import org.springframework.stereotype.Service;
import org.dromara.yuanshidian.domain.bo.Ab02Bo;
import org.dromara.yuanshidian.domain.vo.Ab02Vo;
import org.dromara.yuanshidian.domain.Ab02;
import org.dromara.yuanshidian.mapper.Ab02Mapper;
import org.dromara.yuanshidian.service.IAb02Service;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 原在职人员信息Service业务层处理
 *
 * @author TzhongTop
 * @date 2025-01-24
 */
@RequiredArgsConstructor
@Service
public class Ab02ServiceImpl implements IAb02Service {

    private final Ab02Mapper baseMapper;

    /**
     * 查询原在职人员信息
     *
     * @param id 主键
     * @return 原在职人员信息
     */
    @Override
    public Ab02Vo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询原在职人员信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 原在职人员信息分页列表
     */
    @Override
    public TableDataInfo<Ab02Vo> queryPageList(Ab02Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ab02> lqw = buildQueryWrapper(bo);
        Page<Ab02Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的原在职人员信息列表
     *
     * @param bo 查询条件
     * @return 原在职人员信息列表
     */
    @Override
    public List<Ab02Vo> queryList(Ab02Bo bo) {
        LambdaQueryWrapper<Ab02> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Ab02> buildQueryWrapper(Ab02Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Ab02> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAaa027()), Ab02::getAaa027, bo.getAaa027());
        lqw.eq(StringUtils.isNotBlank(bo.getDwh()), Ab02::getDwh, bo.getDwh());
        lqw.eq(StringUtils.isNotBlank(bo.getGrbh()), Ab02::getGrbh, bo.getGrbh());
        lqw.eq(bo.getGr00() != null, Ab02::getGr00, bo.getGr00());
        lqw.eq(StringUtils.isNotBlank(bo.getDw01()), Ab02::getDw01, bo.getDw01());
        lqw.like(StringUtils.isNotBlank(bo.getDw02()), Ab02::getDw02, bo.getDw02());
        lqw.eq(StringUtils.isNotBlank(bo.getBh()), Ab02::getBh, bo.getBh());
        lqw.eq(bo.getGrid() != null, Ab02::getGrid, bo.getGrid());
        lqw.eq(StringUtils.isNotBlank(bo.getGr01()), Ab02::getGr01, bo.getGr01());
        lqw.eq(StringUtils.isNotBlank(bo.getGr02()), Ab02::getGr02, bo.getGr02());
        lqw.eq(StringUtils.isNotBlank(bo.getGr03()), Ab02::getGr03, bo.getGr03());
        lqw.like(StringUtils.isNotBlank(bo.getGr04()), Ab02::getGr04, bo.getGr04());
        lqw.eq(StringUtils.isNotBlank(bo.getGr05()), Ab02::getGr05, bo.getGr05());
        lqw.eq(StringUtils.isNotBlank(bo.getGr06()), Ab02::getGr06, bo.getGr06());
        lqw.eq(bo.getGr07() != null, Ab02::getGr07, bo.getGr07());
        lqw.eq(bo.getGr08() != null, Ab02::getGr08, bo.getGr08());
        lqw.eq(StringUtils.isNotBlank(bo.getGr09()), Ab02::getGr09, bo.getGr09());
        lqw.eq(bo.getGr10() != null, Ab02::getGr10, bo.getGr10());
        lqw.eq(bo.getGr11a() != null, Ab02::getGr11a, bo.getGr11a());
        lqw.eq(bo.getGr11b() != null, Ab02::getGr11b, bo.getGr11b());
        lqw.eq(bo.getGr11() != null, Ab02::getGr11, bo.getGr11());
        lqw.eq(bo.getGr12() != null, Ab02::getGr12, bo.getGr12());
        lqw.eq(bo.getGr13() != null, Ab02::getGr13, bo.getGr13());
        lqw.eq(bo.getGr14() != null, Ab02::getGr14, bo.getGr14());
        lqw.eq(bo.getGr15() != null, Ab02::getGr15, bo.getGr15());
        lqw.eq(bo.getGr16() != null, Ab02::getGr16, bo.getGr16());
        lqw.eq(bo.getGr17() != null, Ab02::getGr17, bo.getGr17());
        lqw.eq(bo.getGr18() != null, Ab02::getGr18, bo.getGr18());
        lqw.eq(bo.getGr19() != null, Ab02::getGr19, bo.getGr19());
        lqw.eq(bo.getGr20() != null, Ab02::getGr20, bo.getGr20());
        lqw.eq(bo.getGr21() != null, Ab02::getGr21, bo.getGr21());
        lqw.eq(bo.getGr210() != null, Ab02::getGr210, bo.getGr210());
        lqw.eq(bo.getGr211() != null, Ab02::getGr211, bo.getGr211());
        lqw.eq(bo.getGr212() != null, Ab02::getGr212, bo.getGr212());
        lqw.eq(bo.getGr213() != null, Ab02::getGr213, bo.getGr213());
        lqw.eq(bo.getGr214() != null, Ab02::getGr214, bo.getGr214());
        lqw.eq(StringUtils.isNotBlank(bo.getGr22()), Ab02::getGr22, bo.getGr22());
        lqw.eq(StringUtils.isNotBlank(bo.getGr23()), Ab02::getGr23, bo.getGr23());
        lqw.eq(StringUtils.isNotBlank(bo.getYd03()), Ab02::getYd03, bo.getYd03());
        lqw.eq(StringUtils.isNotBlank(bo.getYd08()), Ab02::getYd08, bo.getYd08());
        lqw.eq(bo.getYd10() != null, Ab02::getYd10, bo.getYd10());
        lqw.eq(StringUtils.isNotBlank(bo.getYd11()), Ab02::getYd11, bo.getYd11());
        lqw.eq(bo.getYd12() != null, Ab02::getYd12, bo.getYd12());
        lqw.eq(bo.getGr24() != null, Ab02::getGr24, bo.getGr24());
        lqw.eq(StringUtils.isNotBlank(bo.getGr26()), Ab02::getGr26, bo.getGr26());
        lqw.eq(bo.getGr50() != null, Ab02::getGr50, bo.getGr50());
        lqw.eq(bo.getGr51() != null, Ab02::getGr51, bo.getGr51());
        lqw.eq(bo.getGr52() != null, Ab02::getGr52, bo.getGr52());
        lqw.eq(bo.getGr53() != null, Ab02::getGr53, bo.getGr53());
        lqw.eq(StringUtils.isNotBlank(bo.getGr55()), Ab02::getGr55, bo.getGr55());
        lqw.eq(bo.getGr57() != null, Ab02::getGr57, bo.getGr57());
        lqw.eq(bo.getGr60() != null, Ab02::getGr60, bo.getGr60());
        lqw.eq(StringUtils.isNotBlank(bo.getEd()), Ab02::getEd, bo.getEd());
        lqw.eq(StringUtils.isNotBlank(bo.getEdlp()), Ab02::getEdlp, bo.getEdlp());
        lqw.eq(bo.getEdsj() != null, Ab02::getEdsj, bo.getEdsj());
        lqw.eq(StringUtils.isNotBlank(bo.getDw24()), Ab02::getDw24, bo.getDw24());
        lqw.eq(bo.getSystime() != null, Ab02::getSystime, bo.getSystime());
        lqw.eq(StringUtils.isNotBlank(bo.getTx52()), Ab02::getTx52, bo.getTx52());
        lqw.eq(StringUtils.isNotBlank(bo.getTx57()), Ab02::getTx57, bo.getTx57());
        lqw.eq(StringUtils.isNotBlank(bo.getTx58()), Ab02::getTx58, bo.getTx58());
        lqw.eq(StringUtils.isNotBlank(bo.getPass()), Ab02::getPass, bo.getPass());
        lqw.eq(StringUtils.isNotBlank(bo.getGr61()), Ab02::getGr61, bo.getGr61());
        lqw.eq(StringUtils.isNotBlank(bo.getGr25()), Ab02::getGr25, bo.getGr25());
        lqw.eq(bo.getGr18a() != null, Ab02::getGr18a, bo.getGr18a());
        lqw.eq(bo.getGr19a() != null, Ab02::getGr19a, bo.getGr19a());
        lqw.eq(bo.getGr24a() != null, Ab02::getGr24a, bo.getGr24a());
        lqw.eq(StringUtils.isNotBlank(bo.getSfbj()), Ab02::getSfbj, bo.getSfbj());
        lqw.eq(StringUtils.isNotBlank(bo.getTzqsy()), Ab02::getTzqsy, bo.getTzqsy());
        lqw.eq(StringUtils.isNotBlank(bo.getBz()), Ab02::getBz, bo.getBz());
        lqw.eq(StringUtils.isNotBlank(bo.getTz01()), Ab02::getTz01, bo.getTz01());
        lqw.eq(bo.getZrAmount() != null, Ab02::getZrAmount, bo.getZrAmount());
        lqw.eq(bo.getRyType() != null, Ab02::getRyType, bo.getRyType());
        lqw.eq(bo.getYdType() != null, Ab02::getYdType, bo.getYdType());
        lqw.eq(bo.getZrStartmonth() != null, Ab02::getZrStartmonth, bo.getZrStartmonth());
        lqw.eq(bo.getZrEndmonth() != null, Ab02::getZrEndmonth, bo.getZrEndmonth());
        lqw.eq(bo.getStatus() != null, Ab02::getStatus, bo.getStatus());
        lqw.eq(bo.getDelStatus() != null, Ab02::getDelStatus, bo.getDelStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFileurl()), Ab02::getFileurl, bo.getFileurl());
        lqw.eq(bo.getRefStatus() != null, Ab02::getRefStatus, bo.getRefStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getTableflieurl()), Ab02::getTableflieurl, bo.getTableflieurl());
        return lqw;
    }

    /**
     * 新增原在职人员信息
     *
     * @param bo 原在职人员信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Ab02Bo bo) {
        Ab02 add = MapstructUtils.convert(bo, Ab02.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改原在职人员信息
     *
     * @param bo 原在职人员信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Ab02Bo bo) {
        Ab02 update = MapstructUtils.convert(bo, Ab02.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Ab02 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除原在职人员信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 导出查询原在职人员信息
     * @param ab02 原在职人员信息
     * @return 原在职人员信息集合
     */
    @Override
    public List<Ab02Vo> exportAb02(Ab02Bo ab02, String[] splitID) {
        List<Ab02Vo> list = baseMapper.exportAb02(ab02, splitID);
        List<Ab02Vo> list1 = new ArrayList<>();
        for(Ab02Vo ab02Vo:list){
            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            // 解析开始和截止日期为 YearMonth 对象
            YearMonth start = YearMonth.parse(ab02Vo.getZrStartmonth(), formatter);
            YearMonth end = YearMonth.parse(ab02Vo.getZrEndmonth(), formatter);
            // 计算两个时间之间的月数
            int months = 0;
            YearMonth current = start;
            while (!current.isAfter(end)) {
                months++;
                current = current.plusMonths(1);
            }
            ab02Vo.setGr14((long) months);
            list1.add(ab02Vo);
        }
        return list1;
    }
    /**
     * 根据身份证号查询个人信息
     * @param gr02
     * @return
     */
    @Override
    public Ab02Vo selectgr02(String gr02) {
        return baseMapper.selectgr02(gr02);
    }

    /**
     * 批量修改已经退费的状态
     */
    @Override
    public Boolean updateAb02(String[] splitID) {

        return baseMapper.updateAb02(splitID)>0;
    }

    @Override
    public TableDataInfo<Ab02Vo> customPageList(Ab02Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ab02> lqw = buildQueryWrapper(bo);
        Page<Ab02Vo> result = baseMapper.customPageList(pageQuery.build(), lqw);
        List<Ab02Vo> list =result.getRecords();
        List<Ab02Vo> arrayList= new ArrayList<>();
        for(Ab02Vo ab02Vo:list){
            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            // 解析开始和截止日期为 YearMonth 对象
            if (ab02Vo.getZrStartmonth() != null || ab02Vo.getZrEndmonth() != null) {
                YearMonth start = YearMonth.parse(ab02Vo.getZrStartmonth(), formatter);
                YearMonth end = YearMonth.parse(ab02Vo.getZrEndmonth(), formatter);
                // 计算两个时间之间的月数
                int months = 0;
                YearMonth current = start;
                while (!current.isAfter(end)) {
                    months++;
                    current = current.plusMonths(1);
                }
                ab02Vo.setGr14((long) months);
            }
            arrayList.add(ab02Vo);
        }
        Page<Ab02Vo> ab02VoPage = new Page<>();
        ab02VoPage.setRecords(arrayList);
        return TableDataInfo.build(ab02VoPage);
    }

    @Override
    public List<Ab02Vo> queryAb02List(Ab02Bo bo) {
        LambdaQueryWrapper<Ab02> lqw = buildQueryWrapper(bo);
        List<Ab02Vo> ab02VoList = baseMapper.queryAb02List(lqw);
        List<Ab02Vo> arrayList= new ArrayList<>();
        for(Ab02Vo ab02Vo:ab02VoList){
            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            // 解析开始和截止日期为 YearMonth 对象
            if (ab02Vo.getZrStartmonth() != null || ab02Vo.getZrEndmonth() != null) {
                YearMonth start = YearMonth.parse(ab02Vo.getZrStartmonth(), formatter);
                YearMonth end = YearMonth.parse(ab02Vo.getZrEndmonth(), formatter);
                // 计算两个时间之间的月数
                int months = 0;
                YearMonth current = start;
                while (!current.isAfter(end)) {
                    months++;
                    current = current.plusMonths(1);
                }
                ab02Vo.setGr14((long) months);
            }
            arrayList.add(ab02Vo);
        }
        return arrayList;
    }
}
